/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.member;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.MemberService;
import model.User;
import model.UserService;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import view.member.forms.ProfileStrutsActionForm;

/**
 *
 * @author SONY
 */
public class ProfileStrutsAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // Use Struts actions to record business processing errors.
        ActionErrors errors = new ActionErrors();
        // Store this set in the request scope, in case we need to send the ErrorPage view.
        saveErrors(request, errors);       
        try {
            ProfileStrutsActionForm profile_form = (ProfileStrutsActionForm) form;
            
            UserService user_service = new UserService();
            MemberService member_service = new MemberService();
            if(profile_form.getChangePass()) {
                if(user_service.changePassword(profile_form.getEmail(), profile_form.getPasswordNew())) {
                    System.out.println("update new password successful!");
                } else {
                    System.err.println("Can not update new password.");
                }
            }
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user_member");
            if(member_service.updateInfo(user.getMemberID(), profile_form.getFirstName(), profile_form.getLastName(), profile_form.getAddress(), profile_form.getPhone(), profile_form.getGender())) {
                System.out.println("update member info successful!");
            } else {
                System.err.println("Can not update member info.");
            }
            profile_form.setChangePass(false);
            profile_form.setPasswordOld("");
            profile_form.setPasswordNew("");
            profile_form.setConfirmpassNew("");
            request.setAttribute("success", "Cập Nhật thành công!");
            return mapping.findForward(SUCCESS);
        } catch(Exception e) {
            System.err.println(e);
        }
        return mapping.findForward("error");
    }
}
